home *** CD-ROM | disk | FTP | other *** search
/ Champak 125 / Vol 125 (Damaged).iso / games / rabbit_r.swf / scripts / __Packages / smashing / keithm / Renderable.as < prev    next >
Encoding:
Text File  |  2009-06-09  |  13.5 KB  |  496 lines

  1. class smashing.keithm.Renderable
  2. {
  3.    var mc;
  4.    var baseWidth;
  5.    var baseHeight;
  6.    var hdWidth;
  7.    var hdHeight;
  8.    var radius;
  9.    var doUpdate;
  10.    var owner;
  11.    var __DEF_ASSET;
  12.    var assetID;
  13.    var startX;
  14.    var startY;
  15.    var startZ;
  16.    var depthInfluence;
  17.    var flipH;
  18.    var threedScale;
  19.    var isSpawned;
  20.    var x;
  21.    var nextX;
  22.    var y;
  23.    var nextY;
  24.    var z;
  25.    var __onscreen;
  26.    var isDrawn;
  27.    var isAlive;
  28.    var isHidden;
  29.    var scale;
  30.    var startScale;
  31.    var __fastScale;
  32.    var hdReg;
  33.    var nextZ;
  34.    var TYPE = "element";
  35.    var DRAWTYPE = "element";
  36.    var __DEF_Z = 1000;
  37.    var __3D_SCALE = false;
  38.    var __DEF_WIDTH = 50;
  39.    var __DEF_HEIGHT = 50;
  40.    var __USE_ACTUAL_DIMENSIONS = true;
  41.    var __CAREFUL_DRAW = false;
  42.    var CUSTOMDRAW = false;
  43.    var __FORCE_INITAL_DRAW = false;
  44.    var __DRAW_ON_SPAWN = false;
  45.    var __DEF_HD_WIDTH = 25;
  46.    var __DEF_HD_HEIGHT = 25;
  47.    var __USE_DIMENSIONS_AS_HD = true;
  48.    var __DO_UPDATE = true;
  49.    function Renderable(t_data)
  50.    {
  51.       this.mc = null;
  52.       this.baseWidth = this.__DEF_WIDTH;
  53.       this.baseHeight = this.__DEF_HEIGHT;
  54.       if(this.__USE_DIMENSIONS_AS_HD)
  55.       {
  56.          this.hdWidth = this.baseWidth / 2;
  57.          this.hdHeight = this.baseHeight / 2;
  58.          this.radius = this.hdWidth;
  59.       }
  60.       else
  61.       {
  62.          this.hdWidth = this.__DEF_HD_WIDTH / 2;
  63.          this.hdHeight = this.__DEF_HD_HEIGHT / 2;
  64.          this.radius = this.hdWidth;
  65.       }
  66.       this.setHDRegistration();
  67.       this.doUpdate = this.__DO_UPDATE;
  68.       this.init(t_data);
  69.    }
  70.    function init(t_data)
  71.    {
  72.       if(t_data.owner == undefined)
  73.       {
  74.          trace("ERROR! NO OWNER SENT TO ELEMENT  : " + this.TYPE);
  75.       }
  76.       this.owner = t_data.owner;
  77.       if(t_data.asset == undefined)
  78.       {
  79.          t_data.asset = this.__DEF_ASSET;
  80.       }
  81.       this.assetID = t_data.asset;
  82.       if(t_data.x == undefined)
  83.       {
  84.          t_data.x = 0;
  85.       }
  86.       this.startX = t_data.x;
  87.       if(t_data.y == undefined)
  88.       {
  89.          t_data.y = 0;
  90.       }
  91.       this.startY = t_data.y;
  92.       if(t_data.z == undefined)
  93.       {
  94.          t_data.z = this.__DEF_Z;
  95.       }
  96.       this.startZ = t_data.z;
  97.       if(t_data.depthInfluence == undefined)
  98.       {
  99.          t_data.depthInfluence = 0;
  100.       }
  101.       this.depthInfluence = t_data.depthInfluence;
  102.       if(t_data.camera != undefined)
  103.       {
  104.          this.initScale(t_data.camera);
  105.       }
  106.       else if(this.owner.camera != undefined)
  107.       {
  108.          this.initScale(this.owner.camera);
  109.       }
  110.       else
  111.       {
  112.          trace("ERROR! NO CAMERA SENT TO ELEMENT : " + this.TYPE);
  113.       }
  114.       if(t_data.flip != undefined)
  115.       {
  116.          this.flip();
  117.       }
  118.       else
  119.       {
  120.          this.flipH = false;
  121.       }
  122.       this.threedScale = this.__3D_SCALE;
  123.       this.isSpawned = false;
  124.    }
  125.    function spawn(camera)
  126.    {
  127.       this.isSpawned = true;
  128.       this.reset();
  129.       if(this.__FORCE_INITAL_DRAW)
  130.       {
  131.          smashing.keithm.Viewport.getInstance().draw({element:this});
  132.       }
  133.       else if(this.__DRAW_ON_SPAWN)
  134.       {
  135.          if(camera == undefined)
  136.          {
  137.             trace("ERROR: spawn of " + this.assetID + " failed because __DRAW_ON_SPAWN is true, but no camera was supplied.");
  138.             return undefined;
  139.          }
  140.          this.updateDraw(camera);
  141.       }
  142.    }
  143.    function reset()
  144.    {
  145.       this.x = this.nextX = this.startX;
  146.       this.y = this.nextY = this.startY;
  147.       this.z = this.startZ;
  148.       this.__onscreen = false;
  149.       if(this.mc == undefined || this.mc == null || this.mc == "")
  150.       {
  151.          this.isDrawn = false;
  152.       }
  153.       this.isAlive = true;
  154.       this.isHidden = false;
  155.    }
  156.    function onDraw(t_newmc)
  157.    {
  158.       this.isDrawn = true;
  159.       this.mc = t_newmc;
  160.       if(this.isHidden)
  161.       {
  162.          this.mc._visible = false;
  163.       }
  164.       if(this.__USE_ACTUAL_DIMENSIONS)
  165.       {
  166.          this.baseWidth = this.mc._width;
  167.          this.baseHeight = this.mc._height;
  168.          this.setHDRegistration();
  169.          if(this.__USE_DIMENSIONS_AS_HD)
  170.          {
  171.             this.hdWidth = this.baseWidth / 2;
  172.             this.hdHeight = this.baseHeight / 2;
  173.             this.radius = this.hdWidth;
  174.          }
  175.       }
  176.       if(this.flipH)
  177.       {
  178.          this.mc._xscale = -100;
  179.       }
  180.       this.scale = this.startScale;
  181.       this.mc._x = -1000 - this.baseWidth;
  182.       this.mc._y = -1000 - this.baseHeight;
  183.    }
  184.    function update(dt)
  185.    {
  186.    }
  187.    function move(dt)
  188.    {
  189.    }
  190.    function moveAndRender(camera, dt)
  191.    {
  192.    }
  193.    function updateMoveAndRender(camera, dt)
  194.    {
  195.    }
  196.    function render(camera, dt)
  197.    {
  198.       this.updateDraw(camera);
  199.       if(!this.isDrawn)
  200.       {
  201.          return undefined;
  202.       }
  203.       if(this.threedScale)
  204.       {
  205.          if(this.z < camera.z)
  206.          {
  207.             return undefined;
  208.          }
  209.          this.__fastScale = camera.fl / (this.z - camera.z);
  210.          if(this.scale != this.__fastScale)
  211.          {
  212.             this.scale = this.__fastScale;
  213.             this.mc._xscale = this.mc._yscale = 100 * (1 + (this.scale - this.startScale));
  214.             if(this.flipH)
  215.             {
  216.                this.mc._xscale *= -1;
  217.             }
  218.          }
  219.       }
  220.       this.mc._x = (this.x - camera.x) * this.scale + camera.sc.x;
  221.       this.mc._y = (this.y - camera.y) * this.scale + camera.sc.y;
  222.    }
  223.    function renderDirect(camera)
  224.    {
  225.       this.mc._x = this.x;
  226.       this.mc._y = this.y;
  227.    }
  228.    function initScale(camera)
  229.    {
  230.       this.startScale = this.scale = camera.fl / (this.startZ - camera.z);
  231.    }
  232.    function updateDraw(camera)
  233.    {
  234.       this.__onscreen = false;
  235.       if(this.CUSTOMDRAW)
  236.       {
  237.          this.__onscreen = true;
  238.       }
  239.       else if(this.z >= camera.z)
  240.       {
  241.          if(camera.z < 0)
  242.          {
  243.             this.__onscreen = true;
  244.          }
  245.          else if(Math.abs(this.x - camera.x) < camera.sc.x + this.baseWidth && Math.abs(this.y - camera.y) < camera.sc.y + this.baseHeight && this.z - camera.z < camera.farClip)
  246.          {
  247.             this.__onscreen = true;
  248.          }
  249.       }
  250.       if(!this.isDrawn && this.__onscreen)
  251.       {
  252.          smashing.keithm.Viewport.getInstance().draw({element:this});
  253.       }
  254.       else if(this.isDrawn && !this.__onscreen)
  255.       {
  256.          this.requestErase();
  257.       }
  258.    }
  259.    function onErase(t_viewport)
  260.    {
  261.       this.isDrawn = false;
  262.       this.mc = null;
  263.    }
  264.    function despawn()
  265.    {
  266.       this.isAlive = false;
  267.       this.isSpawned = false;
  268.    }
  269.    function requestErase()
  270.    {
  271.       smashing.keithm.Viewport.getInstance().erase({element:this});
  272.    }
  273.    function requestDepthSort()
  274.    {
  275.       smashing.keithm.Viewport.getInstance().requestGroupDepthSort(this.getDrawType());
  276.    }
  277.    function requestDepthChange()
  278.    {
  279.       smashing.keithm.Viewport.getInstance().requestDepthChange(this);
  280.    }
  281.    function deleteMe()
  282.    {
  283.       false;
  284.    }
  285.    function kill()
  286.    {
  287.       this.isAlive = false;
  288.    }
  289.    function hide()
  290.    {
  291.       if(this.isDrawn)
  292.       {
  293.          this.mc._visible = false;
  294.       }
  295.       this.isHidden = true;
  296.    }
  297.    function show()
  298.    {
  299.       if(this.isDrawn)
  300.       {
  301.          this.mc._visible = true;
  302.       }
  303.       this.isHidden = false;
  304.    }
  305.    function flip()
  306.    {
  307.       this.flipH = true;
  308.       if(this.isDrawn)
  309.       {
  310.          this.mc._xscale = -100;
  311.       }
  312.    }
  313.    function unflip()
  314.    {
  315.       this.flipH = false;
  316.       if(this.isDrawn)
  317.       {
  318.          this.mc._xscale = 100;
  319.       }
  320.    }
  321.    function toggleFlip()
  322.    {
  323.       if(this.flipH)
  324.       {
  325.          this.unflip();
  326.       }
  327.       else
  328.       {
  329.          this.flip();
  330.       }
  331.    }
  332.    function setHDRegistration()
  333.    {
  334.       this.hdReg = {};
  335.       this.hdReg.x = 0;
  336.       this.hdReg.y = 0;
  337.    }
  338.    function runHD_entity_radius(t_target, dt)
  339.    {
  340.       var _loc4_ = this.nextX + this.hdReg.x - (t_target.nextX + t_target.hdReg.x);
  341.       var _loc3_ = this.nextY + this.hdReg.y - (t_target.nextY + t_target.hdReg.y);
  342.       var _loc5_ = Math.sqrt(_loc4_ * _loc4_ + _loc3_ * _loc3_);
  343.       if(_loc5_ <= t_target.radius + this.radius)
  344.       {
  345.          return true;
  346.       }
  347.       return false;
  348.    }
  349.    function runHD_entity_box(t_target, dt)
  350.    {
  351.       if(Math.abs(this.nextX + this.hdReg.x - (t_target.nextX + t_target.hdReg.x)) < this.hdWidth + t_target.hdWidth)
  352.       {
  353.          if(Math.abs(this.nextY + this.hdReg.y - (t_target.nextY + t_target.hdReg.y)) < this.hdHeight + t_target.hdHeight)
  354.          {
  355.             return true;
  356.          }
  357.       }
  358.       return false;
  359.    }
  360.    function runHD_entity_3D(t_target, dt)
  361.    {
  362.       var _loc3_ = this.z - t_target.z;
  363.       var _loc2_ = this.nextZ + this.radius - t_target.z;
  364.       if(_loc3_ * _loc2_ <= 0)
  365.       {
  366.          return this.runHD_entity_radius(t_target,dt);
  367.       }
  368.       return false;
  369.    }
  370.    function runHD_line_sphere(t_lineStart, t_lineEnd, dt)
  371.    {
  372.       var _loc4_ = new smashing.Point3D(t_lineStart.x,t_lineStart.y,0);
  373.       var _loc3_ = new smashing.Point3D(t_lineEnd.x,t_lineEnd.y,0);
  374.       var _loc6_ = _loc4_.x - _loc3_.x;
  375.       var _loc7_ = _loc4_.y - _loc3_.y;
  376.       var _loc2_ = this.getLineSeperation(_loc3_,{x:this.x,y:this.y},_loc6_,_loc7_);
  377.       var _loc5_ = undefined;
  378.       if(_loc2_.distance <= this.radius)
  379.       {
  380.          _loc5_ = new smashing.Point3D(this.x + _loc2_.xSeparation,this.y + _loc2_.ySeparation);
  381.          return true;
  382.       }
  383.       _loc5_.x = _loc5_.y = 0;
  384.       return false;
  385.    }
  386.    function runHD_line_sphere_complex(t_lineStart, t_lineEnd, dt)
  387.    {
  388.       var _loc5_ = new smashing.Point3D(t_lineStart.x,t_lineStart.y,0);
  389.       var _loc7_ = new smashing.Point3D(t_lineEnd.x,t_lineEnd.y,0);
  390.       var _loc8_ = undefined;
  391.       var _loc16_ = this.getDistBetween(_loc5_,{x:this.x,y:this.y});
  392.       var _loc15_ = this.getDistBetween(_loc7_,{x:this.x,y:this.y});
  393.       var _loc11_ = this.getDistBetween(_loc5_,_loc7_);
  394.       var _loc4_ = new smashing.Point3D(_loc5_.x,_loc5_.y);
  395.       var _loc9_ = new smashing.Point3D(_loc7_.x,_loc7_.y);
  396.       var _loc12_ = _loc9_.x - _loc4_.x;
  397.       var _loc13_ = _loc9_.y - _loc4_.y;
  398.       var _loc3_ = _loc12_ / _loc11_;
  399.       var _loc2_ = _loc13_ / _loc11_;
  400.       var _loc10_ = undefined;
  401.       _loc4_.subtractScalarMe(_loc3_ * this.radius,_loc2_ * this.radius);
  402.       _loc9_.addScalarMe(_loc3_ * this.radius,_loc2_ * this.radius);
  403.       var _loc6_ = this.getLineSeperation(_loc5_,{x:this.x,y:this.y},_loc12_,_loc13_);
  404.       _loc12_ = _loc9_.x - _loc4_.x;
  405.       _loc13_ = _loc9_.y - _loc4_.y;
  406.       var _loc14_ = this.getLineSeperation(_loc4_,{x:this.x,y:this.y},_loc12_,_loc13_);
  407.       if(_loc6_.distance <= this.radius)
  408.       {
  409.          if(_loc15_ > _loc11_)
  410.          {
  411.             _loc8_ = Math.sqrt(this.radius * this.radius - _loc14_.distance * _loc14_.distance);
  412.             _loc3_ *= _loc8_;
  413.             _loc2_ *= _loc8_;
  414.             this.x + _loc14_.xSeparation + _loc3_;
  415.             _loc10_ = this.y + _loc14_.ySeparation + _loc2_;
  416.          }
  417.          else
  418.          {
  419.             _loc8_ = Math.sqrt(this.radius * this.radius - _loc6_.distance * _loc6_.distance);
  420.             _loc3_ *= _loc8_;
  421.             _loc2_ *= _loc8_;
  422.             this.x + _loc6_.xSeparation + _loc3_;
  423.             _loc10_ = this.y + _loc6_.ySeparation + _loc2_;
  424.             if(this.getDistBetween(_loc5_,_loc10_) >= _loc11_)
  425.             {
  426.                _loc10_.copy(_loc7_);
  427.             }
  428.          }
  429.          return true;
  430.       }
  431.       _loc10_.x = _loc10_.y = 0;
  432.       return false;
  433.    }
  434.    function getLineSeperation(p0, s, dx, dy)
  435.    {
  436.       var _loc1_ = new Object();
  437.       var _loc5_ = p0.x - s.x;
  438.       var _loc6_ = p0.y - s.y;
  439.       var _loc7_ = dx * dx + dy * dy;
  440.       var _loc2_ = (- (_loc5_ * dx + _loc6_ * dy)) / _loc7_;
  441.       _loc2_ = Math.min(Math.max(_loc2_,0),1);
  442.       _loc1_.xSeparation = _loc5_ + _loc2_ * dx;
  443.       _loc1_.ySeparation = _loc6_ + _loc2_ * dy;
  444.       _loc1_.distance = Math.sqrt(_loc1_.xSeparation * _loc1_.xSeparation + _loc1_.ySeparation * _loc1_.ySeparation);
  445.       return _loc1_;
  446.    }
  447.    function getDistBetween(p0, p1)
  448.    {
  449.       return Math.sqrt((p1.x - p0.x) * (p1.x - p0.x)) + (p1.y - p0.y) * (p1.y - p0.y);
  450.    }
  451.    function hitReact(t_source)
  452.    {
  453.    }
  454.    function getDrawType()
  455.    {
  456.       return this.DRAWTYPE;
  457.    }
  458.    function animate(t_frame, t_subFrame)
  459.    {
  460.       this.mc.gotoAndStop(t_frame);
  461.       if(t_subFrame != undefined)
  462.       {
  463.          this.mc.anim.gotoAndStop(t_subFrame);
  464.       }
  465.    }
  466.    function toString()
  467.    {
  468.       return "Renderable: " + this.TYPE;
  469.    }
  470.    function drawHitBox()
  471.    {
  472.       var _loc3_ = this.hdReg.x;
  473.       var _loc4_ = this.hdReg.y;
  474.       var _loc2_ = this.mc.createEmptyMovieClip("hitbox",1);
  475.       _loc2_.beginFill(16751103,50);
  476.       _loc2_.lineStyle(2,16751103,100);
  477.       _loc2_.moveTo(_loc3_ - this.hdWidth,_loc4_ - this.hdHeight);
  478.       _loc2_.lineTo(_loc3_ + this.hdWidth,_loc4_ - this.hdHeight);
  479.       _loc2_.lineTo(_loc3_ + this.hdWidth,_loc4_ + this.hdHeight);
  480.       _loc2_.lineTo(_loc3_ - this.hdWidth,_loc4_ + this.hdHeight);
  481.       _loc2_.lineTo(_loc3_ - this.hdWidth,_loc4_ - this.hdHeight);
  482.       _loc2_.endFill();
  483.    }
  484.    function drawReg()
  485.    {
  486.       var _loc2_ = this.mc.createEmptyMovieClip("hitbox",1);
  487.       _loc2_.beginFill(16751103,50);
  488.       _loc2_.lineStyle(2,16751103,100);
  489.       _loc2_.moveTo(-5,-5);
  490.       _loc2_.lineTo(5,-5);
  491.       _loc2_.lineTo(5,5);
  492.       _loc2_.lineTo(-5,5);
  493.       _loc2_.lineTo(-5,-5);
  494.    }
  495. }
  496.